package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "C端地址簿相关接口")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "添加地址")
    @PostMapping
    public Result<String> add(@RequestBody AddressBook addressBook){
        log.info("添加地址: {}", addressBook.getId());

        // 业务层
        addressBookService.add(addressBook);
        return Result.success("添加地址成功");
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping()
    @ApiOperation("根据id删除地址")
    public Result<String> deleteById(Long id){
        log.info("根据id删除地址: {}", id);
        addressBookService.deleteById(id);
        return Result.success("删除地址成功");
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation(value = "根据id修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址: {}", addressBook.getId());
        addressBookService.update(addressBook);
        return Result.success("地址修改成功");
    }

    /**
     * 设置当前默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    @ApiOperation(value = "设置默认地址")
    public Result<String> setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址");
        // 业务层
        addressBookService.setDefault(addressBook);
        return Result.success("默认地址设置成功");
    }

    /**
     * 查询当前用户所有地址
     * @return
     */
    @ApiOperation(value = "查询当前用户所有地址")
    @GetMapping("list")
    public Result<List<AddressBook>> list(){
        log.info("查询当前用户所有地址, 当前用户为: {}", BaseContext.getCurrentId());

        // 业务层
        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询地址: {}", id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("default")
    @ApiOperation(value = "查询默认地址")
    public Result<AddressBook> getDefault(){
        log.info("查询默认地址");
        // 业务层
        AddressBook addressBook = addressBookService.getDefault();
        if(addressBook != null){
            return Result.success(addressBook);
        }
        return Result.error("该用户没有设置默认地址");
    }


}
